package com.dong.dongweather;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.dong.dongweather.db.DataBaseAdapter;
import com.dong.dongweather.db.User;

public class RegisteredActivity extends Activity implements View.OnClickListener {

    private EditText etUser;
    private EditText etPass;
    private EditText etPassword;
    private Button btnRegistered;

    private DataBaseAdapter dbAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registered);
        dbAdapter = new DataBaseAdapter(this);
        initView();
    }

    private void initView() {
        etUser = findViewById(R.id.et_user);
        etPass = findViewById(R.id.et_pass);
        etPassword = findViewById(R.id.et_password);
        btnRegistered = findViewById(R.id.btn_Registered);
        btnRegistered.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_Registered:
                //点击注册的时候获取到输入框的值
                String name = etUser.getText().toString().trim();;
                String pass = etPass.getText().toString().trim();
                String password = etPassword.getText().toString().trim();


                //判断输入框的值是否为空
                //判断是否为空
                if (!TextUtils.isEmpty(name) &
                        !TextUtils.isEmpty(pass) &
                        !TextUtils.isEmpty(password)) {
                    //判断两次输入的密码是否一致
                    if (pass.equals(password)) {

                        //注册信息
                        User user = new User();
                        user.setUsername(name);
                        user.setPassword(password);

                        //将注册信息添加到数据库中
                        dbAdapter.add(user);
                        startActivity(new Intent(RegisteredActivity.this,LoginActivity.class));
                        finish();

                    }else {
                        Toast.makeText(this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(this, "输入框不能为空", Toast.LENGTH_SHORT).show();
                }

                break;
        }
    }
}

